<?php
Class DB {
        private $query_num = 0;
//         const DB_HOST='mysql';
//         const DB_USER='lzw';
//         const DB_PW='adminlzw';
//         const DB_NAME='photofromthe1st';
        const DB_HOST='localhost';
        const DB_USER='root';
        const DB_PW='';
        const DB_NAME='photofromthe1st';
        
        function DB($pconnect = 0) {
            $this->connect(self::DB_HOST, self::DB_USER, self::DB_PW, self::DB_NAME, $pconnect);
        }
        #	function DB(DB_HOST, DB_USER, DB_PW, DB_NAME, $pconnect = 0) {
        #		$this->connect(DB_HOST, DB_USER, DB_PW, DB_NAME, $pconnect);
        #	}
        function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0) {
            $pconnect==0 ? @mysql_connect($dbhost, $dbuser, $dbpw) : @mysql_pconnect($dbhost, $dbuser, $dbpw);
            mysql_errno()!=0 && die ('Not connected : ' . mysql_error());
            mysql_query("SET NAMES utf8");
            if($dbname) {
                if (!@mysql_select_db($dbname)){
                    die ('Can\'t connect to db server : ' . mysql_error());
                }
            }
        }
        function close() {
            return mysql_close();
        }
        function select_db($dbname){
            if (!@mysql_select_db($dbname)){
                die ('Can\'t connect to db server : ' . mysql_error());
            }
        }
        function server_info(){
            return mysql_get_server_info();
        }
        function query($SQL,$method='') {
            if($method=='U_B' && function_exists('mysql_unbuffered_query')){
                $query = mysql_unbuffered_query($SQL);
            }else{
                $query = mysql_query($SQL);
            }
            $this->query_num++;

            //echo $SQL.'<br>'.$this->query_num.'<br>';
            if (!$query)  die ('Query Error: ' . $SQL);
            return $query;
        }

        function get_one($SQL){

            $query=$this->query($SQL,'U_B');

            $rs = mysql_fetch_array($query, MYSQL_ASSOC);

            return $rs;
        }

        function pw_update($SQL_1,$SQL_2,$SQL_3){
            $rt=$this->get_one($SQL_1);
            if($rt){
                $this->update($SQL_2);
            } else{
                $this->update($SQL_3);
            }
        }

        function update($SQL) {
            if(function_exists('mysql_unbuffered_query')){
                $query = mysql_unbuffered_query($SQL);
            }else{
                $query = mysql_query($SQL);
            }
            $this->query_num++;

            //echo $SQL.'<br>'.$this->query_num.'<br>';

            if (!$query)  die ('Update Error: ' . $SQL);
            return $query;
        }

        function fetch_array($query, $result_type = MYSQL_ASSOC) {
            return mysql_fetch_array($query, $result_type);
        }

        //BY CAMEL: fetch all the rows in the query and return them with a 2D array
        function get_rows($sql){
            $query=$this->query($sql,'U_B');
            $rs=array();
            while($row=mysql_fetch_array($query, MYSQL_ASSOC)){
                $rs[]=$row;
            }
            return $rs;  
        }

        function affected_rows() {
            return mysql_affected_rows();
        }

        function num_rows($query) {
            $rows = mysql_num_rows($query);
            return $rows;
        }

        function free_result($query) {
            return mysql_free_result($query);
        }

        function insert_id() {
            $id = mysql_insert_id();
            return $id;
        }
        
        function stripslashes_deep($value)
        {
            $value = is_array($value) ?
                        array_map(array($this,'stripslashes_deep'), $value) :
                        stripslashes($value);

            return $value;
        }
        
        function override_magic_quotes($value){
            if (get_magic_quotes_gpc()){
                $value=$this->stripslashes_deep($value);
            }
            return $value;
        }
    }
?>